package csc.fresher.finalgroupfour.service;

import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;

import csc.fresher.finalgroupfour.domain.Account;
import csc.fresher.finalgroupfour.domain.Logging;
import csc.fresher.finalgroupfour.domain.Transaction;
import csc.fresher.finalgroupfour.domain.User;

@Aspect
public class Logger {
	@Autowired
	private UserService userService;

	@Autowired
	private LoggingService logService;
	
	private void log(String data) {
		Logging log = new Logging();
		log.setDate(new Date());
		log.setLogData(data);
		String loginId = userService.getLoginId();
		User user = userService.findUser(loginId);
		log.setUser(user);
		logService.addLogging(log);
	}
	
	@After("execution(* csc.fresher.finalgroupfour.controller.ProfileController.getProfile(..))")
	public void logChangeInfo(JoinPoint point) {
		log("Change info");
	}
	
	@After("execution(* csc.fresher.finalgroupfour.controller.ProfileController.changePass(..))")
	public void logChangePass(JoinPoint point) {
		log("Change password");
	}
	
	@After("execution(* csc.fresher.finalgroupfour.service.TransactionService.makeNewTransaction(..))")
	public void logRequestTransaction(JoinPoint point) {
		Object obs[] = point.getArgs();
		Account sourceAccount = (Account) obs[0];
		Account targetAccount = (Account) obs[1];
		long amount = (long) obs[2];
		StringBuilder builder = new StringBuilder();
		builder.append("Request transaction: From account ");
		builder.append(sourceAccount.getAccountNumber());
		builder.append(" to account ");
		builder.append(targetAccount.getAccountNumber());
		builder.append(" with ");
		builder.append(amount);
		builder.append(" $");
		log(builder.toString());
	}
	
	@After("execution(* csc.fresher.finalgroupfour.service.TransactionService.approveTransaction(..))")
	public void logApproveTransaction(JoinPoint point) {
		Object obs[] = point.getArgs();
		Transaction transaction = (Transaction) obs[0];
		Account sourceAccount = transaction.getSourceAccount();
		Account targetAccount = transaction.getTargetAccount();
		String transactionState = (String) obs[1];
		StringBuilder builder = new StringBuilder();
		builder.append(transactionState.toUpperCase());
		builder.append(" transaction: From account ");
		builder.append(sourceAccount.getAccountNumber());
		builder.append(" to account ");
		builder.append(targetAccount.getAccountNumber());
		builder.append(" with ");
		builder.append(transaction.getAmount());
		builder.append(" $");
		log(builder.toString());
	}
	
	@After("execution(* csc.fresher.finalgroupfour.service.UserService.changeUserState(..))")
	public void logChangeUserState(JoinPoint point) {
		Object obs[] = point.getArgs();
		User user = (User) obs[0];
		StringBuilder builder = new StringBuilder();
		builder.append(user.getUserState().toUpperCase());
		builder.append(" user ");
		builder.append(user.getLoginId());
		log(builder.toString());
	}
}
